/*
 * @Description:
 * @Version: 2.0
 * @Autor: zss
 * @Date: 2025-01-08 16:22:38
 * @LastEditors: zss
 * @LastEditTime: 2025-01-21 16:42:19
 */
import { ifs, ts, Tsrv } from '@/services';
import { N_cAlarmQuery } from './types';

class T_cmd {
  // 主组件箱
  private compBox: ifs.I_compBox;

  // 主组件ID
  private compID: string;

  // 主组件实例
  private self: any;

  private ecBoxDetMap: Map<string, N_cAlarmQuery.I_ecBoxDet> = new Map();

  // 主组件created里初始化
  public init(self: any, compBoxInfo: ifs.I_compBoxInfo) {
    this.self = self;
    this.compBox = compBoxInfo.TcompBox;
    this.compID = compBoxInfo.compID;
  }

  public getTcmd(): T_cmd {
    return this;
  }

  // 读取单次数据
  public readData(codes: string[]) {
    return new Promise<any>((resolve, reject) => {
      Tsrv.standardPostReq<N_cAlarmQuery.I_readResp, any>(
        { ecDetCodes: codes },
        Tsrv.url.scadaRead
      ).then(([readResp, err]) => {
        if (!Tsrv.noValue(err)) {
          this.self.$msg(err);
          reject(err);
        }
        if (!readResp.success) {
          this.self.$msg(readResp.msg);
          reject(readResp.msg);
        }
        resolve(readResp.data);
      });
    });
  }

  // 发送事件
  public sendBusEvent(eventName: string, val: any) {
    Tsrv.reliablySendingBusEvent(eventName, this.self.$bus, 10, 100, ...val);
  }

  public loadFilterList(
    field: string[],
    funName: string,
    filter = {}
  ): Promise<
    Array<{
      [key: string]: any;
    }>
  > {
    const gpfd: ts.T_GPFD = new ts.T_GPFD();
    gpfd
      .setLang(this.self.$i18n.locale || 'en-US')
      .setFunName(funName)
      .setTableName(funName)
      .setDbType(Tsrv.globalVar.dbType.MYSQL);
    gpfd.setFilter(filter).setAgreedFields(...field);

    return new Promise((resolve, reject) => {
      Tsrv.getData<any, ifs.I_GPFD>(gpfd)
        .then(r => {
          const [b, res, rows, msg] = r;
          resolve(rows);
        })
        .catch(err => {
          this.self.$msg(err);
        });
    });
  }
}
interface I_cmd extends T_cmd {}
export { I_cmd, T_cmd };
